'use strict';
const crypto = require('crypto');
const { format } = require('date-fns');
module.exports = app => {
    const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
    // 配置（重要：一定要配置详细，一定要！！！）
    const appraisal = app.model.define('appraisal', {
        id: {
            type: INTEGER(20).UNSIGNED,
            primaryKey: true,
            autoIncrement: true
        },
        appraisalname: {
            type: STRING(200),
            allowNull: false,
            defaultValue: '',
            comment: '测评名称',
        },
        originalprice: {
            type: INTEGER,
            allowNull: false,
            defaultValue: 0,
            comment: '测评价格',
        },
        sellingprice: {
            type: INTEGER,
            allowNull: false,
            defaultValue: 0,
            comment: '测评售价',
        },
        appraisalsellstatus: {
            type: INTEGER(1),
            allowNull: false,
            defaultValue: 1,
            comment: '上架 0禁用1启用'
        },
        appraisalcoverimg: {
            type: STRING(260),
            comment: '主图',
        },
        appraisaltype: {
            type: INTEGER(2),
            allowNull: false,
            comment: '测评类型 1个人成长 2亲密关系 3亲子关系 4情绪管理'
        },
        appraisalimg: {
            type: STRING(260),
            comment: '副图',
        },
        appraisalrecommend: {
            type: INTEGER(1),
            allowNull: false,
            defaultValue: 1,
            comment: '推荐 0禁用1启用'
        },
        appraisaldetailcontent: {
            type: STRING(10000),
            allowNull: true,
            defaultValue: '',
            comment: '测评详细'
        },
        created_at: {
            type: DATE,
            get() {
                const rawValue = this.getDataValue('created_at');
                return rawValue ? format(new Date(rawValue), 'yyyy-MM-dd HH:mm:ss') : null;
            }
        },
        updated_at: DATE
    });

    // // 定义关联关系
    // User.associate = function (model) {
    //     User.hasMany(app.model.Friend, {
    //         as: "bfriends", // 当前用户的被好友
    //         foreignKey: 'friend_id'
    //     });

    //     User.hasMany(app.model.Friend, {
    //         as: "friends", // 当前用户的好友
    //         foreignKey: 'user_id'
    //     });

    //     User.hasMany(app.model.Moment, {
    //         foreignKey: 'user_id'
    //     });
    // }
    // 定义关联关系
    appraisal.associate = function(model) {
        // 一对多
        appraisal.hasMany(app.model.Topic);
    }

    return appraisal;
};